<?php

namespace app\mp\controller;

use app\common\entity\Result;
use app\common\model\Order as OrderModel;
use app\common\model\Vip;
use app\common\service\OrderService;
use app\Request;

/**
 * Class Order
 * @package app\wxapp\controller
 */
class Order extends Base
{
    /**
     * 获取所有启用的VIP等级
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function all()
    {
        return Result::success(Vip::where('state', 1)->order('sort', 'asc')->select());
    }

    /**
     * 判断用户是否已经买过
     * @param Request $request
     * @return \think\Response
     */
    public function order(Request $request)
    {
        $types = ['vip' => OrderModel::VIP, 'topic' => OrderModel::TOPIC, 'video' => OrderModel::VIDEO];
        $srcId = (int)$request->param('id');
        $type = trim($request->param('type'));
        if ($srcId <= 0 || !isset($types [$type])) {
            return Result::error('参数异常');
        }
        $service = new OrderService();
        $result = $service->order($this->user['id'], $srcId, $types[$type]);
        return Result::success($result);
    }
}